package demo1;

public class MysingleList {
   static class ListNode{
        public int val;
        public ListNode next;

       public ListNode(int val, ListNode next) {
           this.val = val;
           this.next = next;
       }

       public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;//永远指向头结点

    public void createList(){
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);
        ListNode node6 = new ListNode(67);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;
        this.head = node1;
    }

    //遍历单链表
    public void show(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val+"    ");
            cur = cur.next;
        }
        System.out.println( );
    }

    //计算链表长度
    public int size(){
        int count = 0;
        ListNode cur = head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
             cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int data){
        ListNode node7 = new ListNode(data);//产生一个新的节点
        node7.next = head;
        head = node7;
    }
    public void addLast(int data){
        ListNode node8 = new ListNode(data);
        //判断是否一个节点也没有
        if(head == null){
            head = node8;
            return;
        }
        ListNode cur = head;
        //找到链表的尾巴节点
        //当cur.next 等于空时，cur就在尾巴节点
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = node8;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        ListNode cur = head;
        ListNode node = new ListNode(data);
        //1.判断index位置的合法性
        if(index < 0 || index > size()){
            throw new IndexOutOfBounds("index的下标不合法！！");

        }
        //2.如果链表为空就是头插法
        if(index == 0){
            addFirst(data);
            return;
        }
        //3.如果index等于链表长度，就是尾插法
        if(index == size()){
            addLast(data);
            return;
        }
        //4.找到index-1的位置
        for (int i = 0; i < index - 1; i++) {
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next = node;

    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        //如果链表为空
        if(head == null){
            return;
        }
        //key是头结点
        if(head.val == key){
            head = head.next;
            return;
        }
        //删除节点
        ListNode pre = searchNode(key);//前驱节点
        if(pre == null){
            System.out.println("没有这个数据！！");
        }
        ListNode del = pre.next;
        pre.next = del.next;

    }
    //找到key的前节点
    public ListNode searchNode(int key){
        ListNode pre = head;
        while (pre.next != null){
            if(pre.next.val == key){
                //删除节点
                return pre;
            }else{
                pre = pre.next;
            }
        }
        return null;
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
    if(head == null){
        return;
    }
    while(head.val == key){
        head = head.next;
    }
    ListNode pre = head;
    ListNode cur = head.next;
    while(cur != null){
        if(cur.val == key){
            pre.next = cur.next;
            cur = cur.next;
        }else {
            pre = cur;
            cur = cur.next;
        }
    }
    }

    //回收链表
    public void clear(){
       //方法一： this.head = null;//将头节点置空，其余的就不见了
        while(head != null){ //方法二：
            ListNode headNext = head.next;
            head = null;
            head = headNext;
        }
    }


    //翻转链表
    public ListNode reverseList(){
        if(head == null){
            return null;
        }
        if(head.next == null){
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;

        }
        return head;
    }

    //两个节点的中间节点
    public ListNode middleNode(){
        if(head == null){
            return null;
        }
        if (head.next == null){
            return head;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return  slow;
    }

    //找到第k个节点
    public ListNode findKthToTail(int k){
        if(k < 0 || head == null){
            System.out.println("k不合理！！");
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        for (int i = 0; i < k - 1; i++) {
            fast = fast.next;
            if(fast == null){
                return null;
            }
        }
        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
    //
    public ListNode partition(int x){
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = head;
        while(cur != null){
            if(cur.val < x){
                if(bs == null){
                    bs = cur;
                    be = cur;
                    //cur = cur.next;
                }else {
                    be.next = cur;
                    be = cur;
                    //cur = cur.next;
                }
                //cur = cur.next;

            }else {
                if(as == null){
                    as = cur;
                    ae = cur;
                    //cur = cur.next;
                }else {
                    ae.next = cur;
                    ae = cur;
                   // cur = cur.next;

                }
                //cur = cur.next;

            }
            cur = cur.next;
        }
        //第一个段落没有数据，直接返回第二个段
        if(bs == null){
            return as;
        }
        be.next = as;
        if(as != null){
            ae.next =null;
        }
        return bs;
    }

}
